package com.itwk.gulimall.search.controller;

import com.itwk.common.exception.BizCodeEnume;
import com.itwk.common.to.search.SearchModel;
import com.itwk.common.utils.R;
import com.itwk.gulimall.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: wangke
 * @Date: 2021/01/26/下午 4:52
 * @Description:
 */
@RestController
@RequestMapping("/search/up")
@Slf4j
public class saveEsController {
    @Autowired
    private SearchService seachService;
    @PostMapping("/bulk")  //远程上架接口，将数据保存进es
    public R saveProductEs(@RequestBody List<SearchModel> searchModels){
        try{
            boolean b = seachService.saveBulk(searchModels);
            if(b){
                return R.ok();
            }else{
                return R.error(BizCodeEnume.UP_EXCEPTION.getCode(), BizCodeEnume.UP_EXCEPTION.getMsg());
            }

        }catch (IOException e){
            log.error("es上架服务出错"+e);
            return R.error(BizCodeEnume.UP_EXCEPTION.getCode(), BizCodeEnume.UP_EXCEPTION.getMsg());
        }


    }
}
